package com.example.app.controller;

import com.example.app.entity.GoodItem;
import com.example.app.service.GoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("goods")
public class GoodController {
    @Autowired
    GoodService goodService;

    @GetMapping({"/{id}"})
    public ResponseEntity<GoodItem> getGood(@PathVariable Long id)
    {
        GoodItem result =goodService.getItem(id);
        if (result == null)
        {
            return ResponseEntity.noContent().build();
        }
        else
        {
            return ResponseEntity.ok(result);
        }
    }

    @GetMapping("")
    public ResponseEntity<List<GoodItem>> findGoods(Integer num, Boolean sold)
    {
        List<GoodItem> result = goodService.findItems(num, sold);
        return ResponseEntity.ok(result);
    }

    @PostMapping("")
    public ResponseEntity<GoodItem> addGood(@RequestBody GoodItem good)
    {
        goodService.addItem(good);
        return ResponseEntity.ok(good);
    }

    @PutMapping("/{id}")
    public ResponseEntity<Void> updateGood(@PathVariable Long id, @RequestBody GoodItem good)
    {
        goodService.updateItem(id, good);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteGood(@PathVariable Long id)
    {
        goodService.deleteItem(id);
        return ResponseEntity.ok().build();
    }
}
